<?php
/**
 * Created by PhpStorm.
 * User: godjarvis
 * Date: 2020/08/06
 * Time: 09:37
 */

namespace App\Model\Dao\DangKou;

use App\Model\Entity\DangKou\Bill;
use Fresh\Annotation\ConnectionField;
use Fresh\Mvc\Model\Dao;
use Fresh\Weird\QueryBuilder;

class BillDao extends Dao
{
    /**
     * @ConnectionField(name="refund")
     */
    public function getClientRefundAmount($cids, $startDate = null, $endDate = null)
    {
        return $this->expr()
            ->from(Bill::class)
            ->inWhere('cid', $cids)
            ->whether(function (QueryBuilder $qb) use ($startDate, $endDate) {
                if ($startDate && $endDate) {
                    $qb->rangeDate2Int($startDate, $endDate, 'date_refund');
                }
            })
            ->query()
            ->pluck('sum(amount)');
    }
}
